<template>
  <div class="login-form">
    <!-- 移除顶部错误提示框 -->

    <div class="form-content">
      <div class="form-fields">
        <div class="form-field">
          <div class="input-container">
            <span class="input-icon">
              <i class="fa-regular fa-envelope"></i>
            </span>
            <input type="text" v-model="form.email" :class="{ 'input-error': errors.email }" class="text-input"
              placeholder="请输入用户名或邮箱">
          </div>
          <!-- 添加内联错误提示 -->
          <div v-if="errors.email" class="error-message">
            <i class="fa-solid fa-circle-exclamation"></i>
            {{ errors.email }}
          </div>
        </div>

        <div class="form-field">
          <div class="input-container">
            <span class="input-icon">
              <i class="fa-solid fa-key"></i>
            </span>
            <input :type="form.showPassword ? 'text' : 'password'" v-model="form.password"
              :class="{ 'input-error': errors.password }" class="text-input" placeholder="请输入您的密码">
            <button type="button" @click="form.showPassword = !form.showPassword" class="toggle-password">
              <i :class="form.showPassword ? 'fa-regular fa-eye-slash' : 'fa-regular fa-eye'"></i>
            </button>
          </div>
          <!-- 添加内联错误提示 -->
          <div v-if="errors.password" class="error-message">
            <i class="fa-solid fa-circle-exclamation"></i>
            {{ errors.password }}
          </div>
        </div>
      </div>

      <div class="remember-forgot-container">
        <div class="checkbox-container">
          <input id="remember" type="checkbox" v-model="form.remember" class="remember-checkbox">
          <label for="remember" class="remember-label">记住我</label>
        </div>
        <a href="#" class="forgot-password">忘记密码？</a>
      </div>
    </div>

    <div class="separator">
      <div class="separator-line"></div>
      <span class="separator-text">或者使用</span>
      <div class="separator-line"></div>
    </div>

    <div class="social-login">
      <a href="#" class="social-button">
        <i class="fa-brands fa-github"></i>
        <span>GitHub</span>
      </a>
      <a href="#" class="social-button">
        <i class="fa-brands fa-weixin"></i>
        <span>微信</span>
      </a>
      <a href="#" class="social-button">
        <i class="fa-brands fa-qq"></i>
        <span>QQ</span>
      </a>
    </div>

    <!-- 固定在底部的按钮 -->
    <div class="fixed-button-container">
      <button @click="handleSubmit" class="login-button">
        <span>登录</span>
        <i class="fa-solid fa-arrow-right"></i>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'

// 定义表单接口
interface LoginForm {
  email: string;
  password: string;
  showPassword: boolean;
  remember: boolean;
}

// 定义错误接口
interface LoginFormErrors {
  email: string;
  password: string;
  [key: string]: string; // 索引签名，允许字符串索引
}

// 表单数据
const form = reactive<LoginForm>({
  email: '',
  password: '',
  showPassword: false,
  remember: false
})

// 错误信息
const errors = reactive<LoginFormErrors>({
  email: '',
  password: ''
})

// 邮箱验证函数
const isValidEmail = (email: string): boolean => {
  const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  return re.test(email)
}

// 表单提交处理
const handleSubmit = () => {
  // 重置所有错误
  Object.keys(errors).forEach(key => {
    errors[key] = ''
  })

  if (!form.email) {
    errors.email = '请输入邮箱或用户名'
  }

  if (!form.password) {
    errors.password = '请输入密码'
  } else if (form.password.length < 6) {
    errors.password = '密码长度不能少于6个字符'
  }

  // 如果没有错误，则发送表单
  if (!errors.email && !errors.password) {
    emit('login', {
      email: form.email,
      password: form.password,
      remember: form.remember
    })
  }
}

// 定义事件
const emit = defineEmits(['login'])
</script>

<style scoped>
* {
  box-sizing: border-box;
}

/* 添加内联错误提示样式 */
.error-message {
  color: #ff6b6b;
  font-size: 0.8rem;
  margin-top: 0.3rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding-left: 0.5rem;
  animation: fadeIn 0.3s ease;
}

.input-error {
  border-color: rgba(255, 107, 107, 0.5) !important;
  background-color: rgba(255, 107, 107, 0.05) !important;
}

/* 移除顶部错误提示框相关样式和动画 */

.login-form {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
}

.form-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-fields {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  /* 减小字段间距 */
  width: 100%;
}

.form-field {
  width: 100%;
}


.forgot-password {
  font-size: 0.875rem;
  color: rgba(180, 180, 250, 0.7);
  text-decoration: none;
  transition: color 0.2s;
}

.forgot-password:hover {
  color: rgba(200, 200, 255, 0.9);
  text-decoration: none;
}

.input-container {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.input-container::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #4F46E5, #10B981);
  transition: all 0.3s ease;
}

.input-container:focus-within::after {
  width: 100%;
  left: 0;
}

.input-icon {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  padding-left: 0.75rem;
  color: rgba(255, 255, 255, 0.5);
}

.text-input {
  width: 100%;
  padding: 0.65rem 0.75rem 0.65rem 2.5rem;
  background-color: rgba(30, 30, 35, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: 4px;
  font-size: 0.875rem;
  color: white;
  transition: all 0.3s;
  outline: none;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.text-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

.text-input:focus {
  border-color: rgba(100, 100, 200, 0.4);
  background-color: rgba(35, 35, 40, 0.7);
  box-shadow:
    0 4px 10px rgba(0, 0, 0, 0.15),
    0 0 0 1px rgba(100, 100, 200, 0.3);
}

.toggle-password {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  padding-right: 0.75rem;
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: color 0.2s;
}

.toggle-password:hover {
  color: rgba(255, 255, 255, 0.8);
}

.remember-forgot-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0.25rem 0;
  width: 100%;
}

.checkbox-container {
  display: flex;
  align-items: center;
}

.remember-checkbox {
  width: 1rem;
  height: 1rem;
  accent-color: #4F46E5;
  margin-right: 0.5rem;
}

.remember-label {
  font-size: 0.875rem;
  color: rgba(255, 255, 255, 0.8);
}

.login-button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  background: linear-gradient(135deg, #4F46E5, #7C3AED);
  color: white;
  font-weight: 500;
  padding: 0.85rem 1rem;
  border: none;
  border-radius: 0.5rem;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  height: 50px;
  box-shadow:
    0 4px 12px rgba(79, 70, 229, 0.3),
    0 0 0 1px rgba(255, 255, 255, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.15);
  position: relative;
  overflow: hidden;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  font-size: 0.9rem;
  animation: neonButtonPulse 6s infinite;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg,
    transparent,
    rgba(255, 255, 255, 0.2),
    transparent);
  transition: 0.5s;
}

.login-button:hover {
  animation: neonButtonPulse 2s infinite;
  transform: translateY(-2px);
  background: linear-gradient(135deg, #5E54F3, #8B4FFF);
  box-shadow:
    0 8px 20px rgba(79, 70, 229, 0.4),
    0 0 0 1px rgba(255, 255, 255, 0.15),
    0 0 15px rgba(79, 70, 229, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
}

.login-button:hover::before {
  left: 100%;
}

.login-button:active {
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(79, 70, 229, 0.4);
}

.login-button i {
  font-size: 1rem;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(3px);
  }
  100% {
    transform: translateX(0);
  }
}

/* 隐藏社交登录区域，因为我们在主界面顶部已经有了 */
.separator,
.social-login {
  display: none;
}

/* 固定按钮容器 */
.fixed-button-container {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background: rgba(30, 30, 40, 0.5); /* 半透明背景 */
  backdrop-filter: blur(5px); /* 模糊效果 */
  z-index: 10;
}

/* 确保输入框具有底部边框和焦点效果 */
.form-group input {
  width: 100%;
  padding: 0.85rem 1rem;
  background: rgba(255, 255, 255, 0.07);
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 0.95rem;
  transition: all 0.3s;
  border-bottom: 2px solid rgba(128, 90, 213, 0.2);
}

.form-group input:focus {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 0 0 2px rgba(128, 90, 213, 0.3);
  border-bottom: 2px solid rgba(128, 90, 213, 0.8);
  outline: none;
}

/* 登录按钮霓虹效果 */
.login-button {
  animation: neonButtonPulse 6s infinite;
}

.login-button:hover {
  animation: neonButtonPulse 2s infinite;
  transform: translateY(-2px);
}
</style>
